package com.ruoyix.flowable.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyix.common.utils.StringUtils;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyix.flowable.domain.bo.BpmFormBo;
import com.ruoyix.flowable.domain.vo.BpmFormVo;
import com.ruoyix.flowable.domain.BpmForm;
import com.ruoyix.flowable.mapper.BpmFormMapper;
import com.ruoyix.flowable.service.IBpmFormService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 流程表单Service业务层处理
 *
 * @author ruoyi
 * @date 2023-11-20
 */
@RequiredArgsConstructor
@Service
public class BpmFormServiceImpl implements IBpmFormService {

    private final BpmFormMapper baseMapper;

    /**
     * 查询流程表单
     */
    @Override
    public BpmFormVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询流程表单列表
     */
    @Override
    public TableDataInfo<BpmFormVo> queryPageList(BpmFormBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BpmForm> lqw = buildQueryWrapper(bo);
        Page<BpmFormVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询流程表单列表
     */
    @Override
    public List<BpmFormVo> queryList(BpmFormBo bo) {
        LambdaQueryWrapper<BpmForm> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BpmForm> buildQueryWrapper(BpmFormBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BpmForm> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getFormKey()), BpmForm::getFormKey, bo.getFormKey());
        lqw.like(StringUtils.isNotBlank(bo.getFormName()), BpmForm::getFormName, bo.getFormName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), BpmForm::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getFormOption()), BpmForm::getFormOption, bo.getFormOption());
        lqw.eq(StringUtils.isNotBlank(bo.getFormFields()), BpmForm::getFormFields, bo.getFormFields());
        return lqw;
    }

    /**
     * 新增流程表单
     */
    @Override
    public Boolean insertByBo(BpmFormBo bo) {
        BpmForm add = BeanUtil.toBean(bo, BpmForm.class);
        add.setStatus("1");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改流程表单
     */
    @Override
    public Boolean updateByBo(BpmFormBo bo) {
        BpmForm update = BeanUtil.toBean(bo, BpmForm.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BpmForm entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除流程表单
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
